package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.DeliveryCommandSheetLinesEntity;
import huzofw.service.impl.GenericManagerImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.com.huzo.repository.trade.DeliveryCommandSheetLinesRepository;
import cn.com.huzo.service.trade.DeliveryCommandSheetLinesManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.MapUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发货发令单明细管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("DeliveryCommandSheetLinesManager")
public class DeliveryCommandSheetLinesManagerImpl extends GenericManagerImpl<DeliveryCommandSheetLinesEntity, String>
        implements DeliveryCommandSheetLinesManager {

    @Resource(name = "DeliveryCommandSheetLinesRepository")
    private DeliveryCommandSheetLinesRepository _dao_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchByHeaderId(Long headerId) throws Exception {
        Map<String, Object> rtnValue = new HashMap();
        rtnValue.put("LinesList", _dao_lines.fetchByHeaderId(headerId));

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<DeliveryCommandSheetLinesEntity> fetchByHeaderIdAndStyleNo(Long headerId, String styleNo) throws Exception {
        return _dao_lines.fetchByHeaderIdAndStyleNo(headerId, styleNo);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(List<DeliveryCommandSheetLinesEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();
        Integer rowsAffected = _dao_lines.batchUpdate(itemList);
        /*
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(itemList.get(0).getUpdatedBy(),
                    "贸易信息→发货发令单明细", "单据号：" + itemList.get(0).getBillNo(),
                    "更新明细");
        }*/
        _result.put("LinesList", _dao_lines.fetchByHeaderId(itemList.get(0).getHeaderId()));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        Integer rowsAffected = _dao_lines.delete(MapUtils.getString(criterion, "LinesId"));
        if (rowsAffected > 0) {
            // 删除不存在的款号排序数据
            _dao_lines.deleteLinesStyleNoOrder(MapUtils.getLong(criterion, "HeaderId"));
            // 审计日志
            //writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
            //        "贸易信息→发货发令单明细", "明细数据（" + MapUtils.getString(criterion, "LinesData") + "）",
            //        "删除明细");
        }

        _result.put("LinesList", _dao_lines.fetchByHeaderId(MapUtils.getLong(criterion, "HeaderId")));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchDelete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        ObjectMapper mapper = new ObjectMapper();
        List<DeliveryCommandSheetLinesEntity> itemList = mapper.convertValue(MapUtils.getObject(criterion, "ItemList"), new TypeReference<List<DeliveryCommandSheetLinesEntity>>() {
        });
        Integer rowsAffected = _dao_lines.batchDelete(itemList);

        if (rowsAffected > 0) {
            // 删除不存在的款号排序数据
            _dao_lines.deleteLinesStyleNoOrder(MapUtils.getLong(criterion, "HeaderId"));
            // 审计日志
            //writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
            //        "贸易信息→发货发令单明细", "单据号：" + MapUtils.getString(criterion, "BillNo"),
            //        "批量删除明细");
        }

        _result.put("LinesList", _dao_lines.fetchByHeaderId(MapUtils.getLong(criterion, "HeaderId")));

        return _result;
    }

    // <editor-fold defaultstate="collapsed" desc="款号顺序操作">
    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public List<Map<String, Object>> fetchLinesStyleNoOrderByHeaderId(Long headerId) throws Exception {
        return _dao_lines.fetchLinesStyleNoOrderByHeaderId(headerId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public Map<String, Object> updateLinesStyleNoOrder(List<Map<String, Object>> itemList) throws Exception {
        _dao_lines.updateLinesStyleNoOrder(itemList);

        // 返回值
        Map<String, Object> _result = new HashMap();
        _result.put("LinesList", _dao_lines.fetchByHeaderId(MapUtils.getLong(itemList.get(0), "HeaderId")));

        return _result;
    }
    // </editor-fold>
}
